package com.study.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Description: 有界流处理
 * @Author: LiuQun
 * @Date: 2022/7/24 15:55
 */
public class BoundedStreamWordCount {
    public static void main(String[] args) throws Exception {
        // 1.创建流式执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2.读取文件
        DataStreamSource<String> lineDSS = env.readTextFile("input/words.txt");
        // 3.转换数据格式
        SingleOutputStreamOperator<Tuple2<String, Long>> wordAnd1Tuple = lineDSS.flatMap(
                (String line, Collector<Tuple2<String, Long>> out) -> {
                    String[] wordArr = line.split(" ");
                    for (String word : wordArr) {
                        out.collect(Tuple2.of(word, 1L));
                    }
                }
        ).returns(Types.TUPLE(Types.STRING, Types.LONG));//因为泛型擦除，因此需要在返回时指定数据类型
        // 4.分组
        KeyedStream<Tuple2<String, Long>, String> wordAnd1KS = wordAnd1Tuple.keyBy(data -> data.f0);
        // 5.求和
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = wordAnd1KS.sum(1);
        // 6.打印
        sum.print();
        // 7.执行
        env.execute();
    }
}
